#ifndef _OCTREE_MANAGER_H
#define _OCTREE_MANAGER_H

#include "../stdafx.h"
#include "../objects/entity.h"
#include "../memory/toolkit.h"
#include <vector>

namespace octree
{

class COCTreeNode;
class COCTree;
//class CEntity;
struct entity_mask_info;

class COCTreeManager
{
private:
	COCTreeManager();
	static std::set<COCTreeNode * const> active_nodes;
	static std::set<COCTreeNode * const> set_nodes;
	static std::set<COCTreeNode * const> removed_nodes;

	static std::set<COCTreeNode * const> subdivided_nodes;
	static std::set<COCTreeNode * const> compacted_nodes;

	static std::vector<std::pair<CEntity *, CEntity *> > colliding_entities;

	static entity_mask_info * tmp_buff1;
	static entity_mask_info * tmp_buff2;

public:
	static bool allowSubdividedPush;
	static entity_mask_info * getTmpBuff1(){return tmp_buff1;}
	static entity_mask_info * getTmpBuff2(){return tmp_buff2;}
	static void init(unsigned long tmp_buff_sz = 4192304);
	
	static const std::vector<std::pair<CEntity *, CEntity *> > & getCollidingEntities(){return colliding_entities;}

	static void addToSubdivised(COCTreeNode * const node)
	{
		subdivided_nodes.insert(node);
	}
	static void removeFromSubdivised(COCTreeNode * const node)
	{
		subdivided_nodes.erase(node);
	}
	static void addToCompacted(COCTreeNode * const node)
	{
		compacted_nodes.insert(node);
	}
	static void removeFromCompacted(COCTreeNode * const node)
	{
		compacted_nodes.erase(node);
	}
	static void setActive(COCTreeNode * const node)
	{
		set_nodes.insert(node);
	}
	static void removeFromActive(COCTreeNode * const node)
	{
		removed_nodes.insert(node);
	}
	static void addToCollidingList(CEntity * collider, CEntity * target)
	{
		colliding_entities.push_back(std::make_pair<CEntity *, CEntity *>(collider, target));
	}

	static COCTree * const getRoot();
	static void drawActiveNodes(void(*drawFunc)(const __m128 & pos));
	static void updateActiveNodes(void);
	static void processCollisions(void);
};

} // namespace octree
#endif // _OCTREE_MANAGER_H